<?php
namespace app\admin\controller;

use app\common\controller\AdminController;
use app\common\logic\AdminLogic;
use gdbase\DatabaseSchema;
use Phinx\Db\Adapter\AdapterFactory;
use Phinx\Db\Adapter\MysqlAdapter;
use Phinx\Db\Table;
use think\Db;
use think\facade\Cache;
use think\facade\Env;
use think\migration\Migrator;

class Index extends AdminController
{
    public function initialize()
    {
        $this->noCheckAction = [
            'set_project',
            'get_umeditor_config',
            'select_photo',
            'clear_cache',
        ];
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    public function index()
    {
        return $this->fetch();
    }

    /**
     * 设置小区（项目） web访问
     */
    public function set_project(){
        $projectId = input('id');
        // 判断是否 为 - 1 超级项目管理，不然返回不允许切换项目
        if (false === $this->isChangeProject($projectId)) {
            $this->error('切换失败，本账号不允许切换');
        }

        if($this->setProjectId($projectId)){
            $this->success('切换成功');
        }
        else{
            $this->error('切换失败，找不到小区（项目）');
        }
    }

    public function jian(){
        return $this->fetch();
    }

    /**
     * 富文本编辑设置获取
     * @return \think\response\Json
     */
    public function get_umeditor_config(){
        $config = [
//            'UMEDITOR_HOME_URL' => UMEDITOR_HOME_URL,
            'filterRules' => [],
            'imageFieldName' => "upfile",
            'imagePath' => '',
            'imageUrl' => '',
            'zIndex' => '1100',
            'inputXssFilter' => true,
            'outputXssFilter' => true,
            'toolbar' => [
                "source | undo redo | bold italic underline strikethrough | superscript subscript | forecolor backcolor | removeformat |",
                "insertorderedlist insertunorderedlist | selectall cleardoc paragraph | fontfamily fontsize",
                "| justifyleft justifycenter justifyright justifyjustify |",
                "link unlink | emotion image video  | map",
                "| horizontal print preview fullscreen",
                "drafts",
            ],
            'whiteList' => [
                'a' => ['target', 'href', 'title', 'style', 'class', 'id'],
                'abbr' => ['title', 'style', 'class', 'id'],
                'address' => ['style', 'class', 'id'],
                'area'  =>  ['shape', 'coords', 'href', 'alt', 'style', 'class', 'id'],
                'article' => ['style', 'class', 'id'],
                'aside' =>  ['style', 'class', 'id'],
                'audio' =>  ['autoplay', 'controls', 'loop', 'preload', 'src', 'style', 'class', 'id'],
                'b' =>   ['style', 'class', 'id'],
                'bdi'=>    ['dir'],
                'bdo'=>    ['dir'],
                'big'=>    [],
                'blockquote'=> ['cite', 'style', 'class', 'id'],
                'br'=>     [],
                'caption'=> ['style', 'class', 'id'],
                'center'=> [],
                'cite'=>   [],
                'code'=>   ['style', 'class', 'id'],
                'col'=>    ['align', 'valign', 'span', 'width', 'style', 'class', 'id'],
                'colgroup'=> ['align', 'valign', 'span', 'width', 'style', 'class', 'id'],
                'dd'=>     ['style', 'class', 'id'],
                'del'=>    ['datetime', 'style', 'class', 'id'],
                'details'=> ['open', 'style', 'class', 'id'],
                'div'=>    ['style', 'class', 'id'],
                'dl'=>     ['style', 'class', 'id'],
                'dt'=>     ['style', 'class', 'id'],
                'em'=>     ['style', 'class', 'id'],
                'embed'=>  ['style', 'class', 'id', '_url', 'type', 'pluginspage', 'src', 'width', 'height', 'wmode', 'play', 'loop', 'menu', 'allowscriptaccess', 'allowfullscreen'],
                'font'=>   ['color', 'size', 'face', 'style', 'class', 'id'],
                'footer'=> ['style', 'class', 'id'],
                'h1'=>     ['style', 'class', 'id'],
                'h2'=>     ['style', 'class', 'id'],
                'h3'=>     ['style', 'class', 'id'],
                'h4'=>     ['style', 'class', 'id'],
                'h5'=>     ['style', 'class', 'id'],
                'h6'=>     ['style', 'class', 'id'],
                'header'=> ['style', 'class', 'id'],
                'hr'=>     ['style', 'class', 'id'],
                'i'=>      ['style', 'class', 'id'],
                'iframe'=> ['style', 'class', 'id', 'src', 'frameborder', 'data-latex'],
                'img'=>    ['src', 'alt', 'title', 'width', 'height', 'style', 'class', 'id', '_url'],
                'ins'=>    ['datetime', 'style', 'class', 'id'],
                'li'=>     ['style', 'class', 'id'],
                'mark'=>   [],
                'nav'=>    [],
                'ol'=>     ['style', 'class', 'id'],
                'p'=>      ['style', 'class', 'id'],
                'pre'=>    ['style', 'class', 'id'],
                's'=>      [],
                'section'=>[],
                'small'=>  ['style', 'class', 'id'],
                'span'=>   ['style', 'class', 'id'],
                'sub'=>    ['style', 'class', 'id'],
                'sup'=>    ['style', 'class', 'id'],
                'strong'=> ['style', 'class', 'id'],
                'table'=>  ['width', 'border', 'align', 'valign', 'style', 'class', 'id'],
                'tbody'=>  ['align', 'valign', 'style', 'class', 'id'],
                'td'=>     ['width', 'rowspan', 'colspan', 'align', 'valign', 'style', 'class', 'id'],
                'tfoot'=>  ['align', 'valign', 'style', 'class', 'id'],
                'th'=>     ['width', 'rowspan', 'colspan', 'align', 'valign', 'style', 'class', 'id'],
                'thead'=>  ['align', 'valign', 'style', 'class', 'id'],
                'tr'=>     ['rowspan', 'align', 'valign', 'style', 'class', 'id'],
                'tt'=>     ['style', 'class', 'id'],
                'u'=>      [],
                'ul'=>     ['style', 'class', 'id'],
                'svg'=>    ['style', 'class', 'id', 'width', 'height', 'xmlns', 'fill', 'viewBox'],
                'video'=>  ['autoplay', 'controls', 'loop', 'preload', 'src', 'height', 'width', 'style', 'class', 'id'],
            ]
        ];

        return json($config);
    }

    /**
     * 图片选择框
     * @return mixed
     * @throws \Exception
     */
    public function select_photo(){
        $type = input('type',2);
        $callBackName = input('cb','uploadMultiImageCallBack');
        $fieldname = input('fieldname','');

        if (2 == $type) {
            // 读取图片
            $alj = new AdminLogic();
            $pageIndex = input('p',1);
            $pageSize = 10;
            $this->assign('photos',$alj->_getAttachment($pageIndex,$pageSize));
            $this->assign('base_url',Env::get('LOCAL_BASE_URL'));
            $param = input('param.');
            if(empty($param['search'])){
                $search = [
                    'type' => $type,
                    'cb' => $callBackName,
                    'fieldname' => $fieldname,
                ];
            }
            else{
                $search = $param['search'];
            }

            $totalRows = $alj->_getCountByWhere([],0,'Attachment');
            $actionName = 'select_photo';
            $offset = ($pageIndex - 1) * $pageSize;

            $totalPages = ceil($totalRows / $pageSize);
            $pageUrl = url($actionName) . '?t=' . time();
            if(!empty($search)){
                $canshu = [];
                foreach ($search as $key => $item){
                    $canshu[$key] = $item;
                }
                $pageUrl .= '&' . http_build_query($canshu);
            }
            $pagerInfo = [
                'offset' => $offset,
                'pageIndex' => $pageIndex,
                'pageSize' => $pageSize,
                'totalPages' => $totalPages,
                'totalRows' => $totalRows,
                'pageUrl' => $pageUrl,
            ];

            $this->assign($pagerInfo);
        }

        $this->assign('type',$type);
        $this->assign('callBackName',$callBackName);
        $this->assign('fieldName',$fieldname);
        return $this->fetch();
    }

    /**
     * 清除缓存
     */
    public function clear_cache()
    {
        Cache::clear('system');
//        Cache::clear();

        $this->success('清除成功');
    }

    public function test() {
        $table = new DatabaseSchema('member_level', []);
        $table->removeColumn('test_11');
        $table->removeColumn('test_22');
        $table->save();

    }
}
